import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class test {
    private static final int TOTAL_PROBLEMS = 50;
    private static final int MAX_OPERAND = 100;

    public static void main(String[] args) {
        Random random = new Random();
        Set<String> uniqueProblems = v3.generateUniqueProblems(random, TOTAL_PROBLEMS);

        // 测试生成的算式数量
        if (uniqueProblems.size() != TOTAL_PROBLEMS) {
            System.err.println("测试失败：期望生成 " + TOTAL_PROBLEMS + " 个算式，但实际生成了 " + uniqueProblems.size() + " 个。");
            return;
        }

        // 捕获标准输出
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));

        // 调用printProblems方法
        v3.printProblems(uniqueProblems);

        // 恢复标准输出
        System.setOut(System.out);

        // 将输出转换为字符串
        String output = outContent.toString();

        // 使用Scanner来解析输出
        Scanner scanner = new Scanner(output);
        int count = 0;
        int lineCount = 0;

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            Scanner lineScanner = new Scanner(line);
            while (lineScanner.hasNext()) {
                String problem = lineScanner.next();
                // 检查算式格式
                if (!problem.matches("\\d+ \\+ \\d+ = \\d+|\\d+ - \\d+ = \\d+")) {
                    System.err.println("测试失败：算式格式不正确 - " + problem);
                    return;
                }
                count++;
            }
            lineScanner.close();
            lineCount++;
        }
        scanner.close();

        // 检查是否每行都打印了5个算式
        if (lineCount != TOTAL_PROBLEMS / v3.PROBLEMS_PER_LINE) {
            System.err.println("测试失败：期望每行打印 " + v3.PROBLEMS_PER_LINE + " 个算式，但实际不是这样。");
            return;
        }
        // 检查是否打印了正确的算式数量
        if (count != TOTAL_PROBLEMS) {
            System.err.println("测试失败：期望打印 " + TOTAL_PROBLEMS + " 个算式，但实际打印了 " + count + " 个。");
            return;
        }

        System.out.println("测试通过：所有测试都成功。");
    }
}
